//
//  Cache.m
//  MyProject
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//

#import "Cache.h"

@implementation Cache


//      单例方法
+ (instancetype)shareDataBaseHandle{
    
    static Cache *cheche;
    static dispatch_once_t onceToken;
    
    //      保证线程上的绝对安全
    dispatch_once(&onceToken, ^{
        cheche = [[Cache alloc] init];
    });
    return cheche;
    
}




#pragma mark 开数据库
- (void)openDB {
    
    //      1. 沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //      2. 拼接路径                                              本文件名
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"foodSay.sqlite"];
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);

    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
}




#pragma mark 建表
- (void)createTable {
    
    //    NSString *sqlStr = @"create table if not exists movie(movieId text primary key , movieName text, pic_url text)";
    
    //                                                表名           NSString -> text 类型   并且需要写一个 flag 区分表名
    NSString *sqlStr = @"create table if not exists foodSay(post_num text, huodong_type text, huodong_id text,img text, period text, end_date text, smalltext text, flag integer)";
    
    
    //      执行sql语句
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    } else {
        NSLog(@"创建表失败");
    }
}



#pragma mark 查询
- (SelectMovie)isHaveInTable:(ActivityModel *)model{
    
    //      除了openDB, 都先写SQL语句
    //    NSString *sqlStr = [NSString stringWithFormat:@"select * from movie where movieId = '%@'", movie.movieId];
    
    //      flag 用来判断表是不是唯一
    NSString *sqlStr = [NSString stringWithFormat:@"select * from foodSay where huodong_id = '%@' and flag = '1'", model.huodong_id];
    
    
    
    //      跟随指针
    sqlite3_stmt *stmt = nil;
    
    //      执行SQL语句
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
    if (result == SQLITE_OK) {
        
        NSLog(@"查询成功");
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            sqlite3_finalize(stmt);
            return InTable;
        } else {
            
            sqlite3_finalize(stmt);
            return NotInTable;
        }
    } else {
        
        sqlite3_finalize(stmt);
        NSLog(@"查询失败");
        return SelectError;
    }
    
    return YES;
}

#pragma mark 收藏(添加)
- (void)saveMovieInDB:(ActivityModel *)model {
    
    //    NSString *sqlStr = [NSString stringWithFormat:@"insert into movie(movieId, movieName, pic_url) values ('%@', '%@', '%@')", movie.movieId, movie.movieName, movie.pic_url];
    
    //      写的时候可以点击像手一样的工具查看自己的查询文字是否有错
    NSString *sqlStr = [NSString stringWithFormat:@"insert into foodSay(post_num, huodong_type, huodong_id,img, period, end_date, smalltext, flag) values ('%@', '%@', '%@', '%@','%@', '%@', '%@', '1')", model.post_num, model.huodong_type, model.huodong_id, model.img, model.period, model.end_date, model.smalltext];
    
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
    
}

#pragma mark 取消收藏

- (void)deleteMovieInDB:(ActivityModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"update foodSay set flag = '0' where huodong_id = '%@'", model.huodong_id];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    
}


#pragma mark - 查询整个表
- (NSMutableArray *)selectAllModel {
    
    NSMutableArray *modelArray = [NSMutableArray array];
    
    NSString *sqlStr = @"select * from foodSay where flag = '1'";
    
    //      跟随指针
    sqlite3_stmt *stmt = nil;
    
    //      执行查找
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            ActivityModel *model= [[ActivityModel alloc] init];
            
            model.post_num = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            model.huodong_type = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            model.huodong_id = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            
            //      存在数据库为 nsstring 类型, 因为本身是字典, 所以取出来是要强转成字典
//            model.img = (NSMutableDictionary *)[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];

            model.period = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
            model.end_date = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 5)];
            model.smalltext = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 6)];
            [modelArray addObject:model];

            [model release];
        }
    }
    sqlite3_finalize(stmt);
//    NSLog(@"%@", modelArray);
    return modelArray;
    
}







@end



























